package com.party.core.service.crowdfund.impl;

import com.party.common.constant.Constant;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.member.PartnerMember;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IProjectRefundService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 众筹退款实现
 * Created by wei.li
 *
 * @date 2017/4/28 0028
 * @time 11:47
 */

@Service
public class ProjectRefundService implements IProjectRefundService {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IPartnerMemberService partnerMemberService;


    protected static Logger logger = LoggerFactory.getLogger(ProjectRefundService.class);
    /**
     * 众筹项目退款
     * @param project 众筹信息
     */
    @Override
    @Transactional
    public void refund(Project project) {
        // 众筹项目的冗余字段统计数减少
        TargetProject targetProject = targetProjectService.findByProjectId(project.getId());
        Activity activity = activityService.get(targetProject.getTargetId());
        if (Constant.IS_CROWFUND_ING.equals(project.getIsSuccess())){
            logger.info("众筹项目退款--------------getBeCrowdfundNum:::"+(activity.getBeCrowdfundNum() - 1));
            activity.setBeCrowdfundNum(activity.getBeCrowdfundNum() - 1);
        }
        else if (Constant.CROWDFUND_PROJECT_SUCCESS.equals(project.getIsSuccess())){
            logger.info("众筹项目退款--------------getCrowdfundedNum:::"+(activity.getCrowdfundedNum() - 1));
            activity.setCrowdfundedNum(activity.getCrowdfundedNum() - 1);
        }
        activityService.update(activity);

        project.setIsSuccess(Constant.CROWDFUND_PROJECT_REFUNDED);//退款成功
        project.setRefundState(Constant.CROEDFUND_PROJECT_FAIL);
        projectService.update(project);

        try {
            PartnerMember partnerMember = partnerMemberService.findByPartnerAndMember(activity.getMember(), project.getAuthorId());
            memberPersonalCountBizService.countOne(partnerMember);
        } catch (Exception e) {
            logger.error("退款时实时更新众筹数量异常 ", e);
        }

        new Thread(() -> {
            Integer beCrowdfundNum = projectService.countByActivity(activity.getId(), Constant.IS_CROWFUND_ING);
            logger.info("众筹项目退款--------------getBeCrowdfundNum:::" + beCrowdfundNum);
            activity.setBeCrowdfundNum(beCrowdfundNum);

            Integer crowdfundedNum = projectService.countByActivity(activity.getId(), Constant.IS_CROWFUND_SUCCESS);
            logger.info("众筹项目退款--------------getCrowdfundedNum:::" + crowdfundedNum);
            activity.setCrowdfundedNum(crowdfundedNum);
            activityService.update(activity);
        }).start();
    }

    @Override
    public void refundNoSuccess(Project project) {
        project.setRefundState(Constant.CROWDFUND_PROJECT_SUCCESS);//project 新加的字段refundState  退款未成功（针对报名人员）
        logger.info("project :"+project.toString());
        boolean update = projectService.update(project);
        logger.info("update1111:"+update);
    }
}
